var categoryDAL = require('../DAL/categoryDAL');
var productDAL = require('../DAL/productDAL');
var managerDAL = require('../DAL/managerDAL');
var orderDAL = require('../DAL/orderDAL');
var customerDAL = require('../DAL/customerDAL');
//面向对象编程引入model
var ProductInfo = require('../Model/ProductInfo');
//调用自定义文件上传模块
var fileuploadHelper = require('../util/fileuploadHelper');
//流程控制模块
var async = require('async');

function managerController(app) {
	// body...
	/* GET home page. */
	//app.get方法第一个参数是我们添加网站可以请求的地址
	//后台登录页面
	app.get('/manager/login', function(req, res) {
		//后台登录地址
		res.render('manager/manager_login');

	});

	//退出登录后台
	app.get('/manager/logout', function(req, res) {

		req.session.manager = null;
		res.redirect('/manager/login');

	});

	app.post('/manager/validateLogin', function(req, res) {
		var loginname = req.body.username;
		var loginpass = req.body.password;
		var validateCode = req.body.validateCode.toUpperCase();

		//*******************************
		//测试数据，默认登录，避免测试繁琐登录，正式部署的时候再恢复
		//		loginname = 'admin';
		//		loginpass = '123456';
		//
		//		//验证码通过进行用户登录 
		//		managerDAL.validateLogin(loginname, loginpass, function(message, userInfo, status) {
		//			//将管理员信息存入session
		//			req.session.manager = userInfo;
		//			res.redirect('/manager/');
		//
		//		});

		//****************正式部署时，请注释此部分内容***************

		//验证输入
		if(loginname.trim() == "" || loginpass.trim() == "" || validateCode.trim() == "") {
			
			res.redirect('/manager/login');
			return false;
		}

		//获取验证码
		var code = req.session.CAPTCHA_KEY;

		//console.log(code+','+validateCode);

		if(validateCode == code) {
			//验证码通过进行用户登录 
			managerDAL.validateLogin(loginname, loginpass, function(message, userInfo, status) {
				//将管理员信息存入session
				req.session.manager = userInfo;
				res.redirect('/manager/');

			});

		} else {
			//return false;
			res.redirect('/manager/login');
		}

	});

	//后台首页
	app.get('/manager/', function(req, res) {

		//验证登录
		if(!req.session.manager) {
			res.redirect('/manager/login');
		}

		//应用async模块的api:parallel处理并行无关联操作
		async.parallel({ //控制流程1:管理员用户信息			
			userInfo: function(done) {
				var userInfo = req.session.manager;
				done(null, userInfo);
			}
		}, function(err, results) {
			//后台登录地址
			res.render('manager/index', {
				userInfo: results.userInfo
			});

		});

	});

	//产品列表管理
	app.get('/manager/manager_productlist', function(req, res) {
		//后台登录地址
		res.redirect('/manager/manager_productlist/1');

	});

	//管理产品列表分页 id为页码信息
	app.get('/manager/manager_productlist/:id', function(req, res) {
		var currentpage = parseInt(req.params.id);

		//验证登录
		if(!req.session.manager) {
			res.redirect('/manager/login');
		}

		//分页方法
		//定义分页参数
		//var currentpage = 1; //当前页
		var pagercounts = 1; //页面总数
		var previouspage = 1; //上一页
		var nextpage = 1;
		//设置分页 每页显示数据条数
		var pageSize = 10;
		//应用async模块的api:parallel处理并行无关联操作
		//async的waterfall模块， 每一步执行时需要由上一步执行的结果当做参数
		async.parallel({
				goodListPager: function(done) {
					//处理逻辑
					productDAL.getGoodsListPager(currentpage, pageSize, function(goodslist) {

						done(null, goodslist);
					});
				},
				goodsCounts: function(done) {
					//处理逻辑
					productDAL.getGoodsCounts(function(count) {
						console.log('流程2：' + count);
						done(null, count);
					});
				},
				userInfo: function(done) {
					var userInfo = req.session.manager;
					done(null, userInfo);
				}
			},
			function(error, results) {
				//console.log('流程3：');
				//console.log('****'+results.goodsCounts+',');
				//将上面两步数据操作并行处理，然后在这里一起操作，提供运行效率
				var counts = parseInt(results.goodsCounts);

				//分页信息处理
				//计算总页数
				if((counts % pageSize) > 0) {
					//有余数，数据不足一整页
					pagercounts = parseInt(counts / pageSize) + 1;
				} else {
					//数据正好填满整页
					pagercounts = parseInt(counts / pageSize);
				}
				//设置上一页
				if(currentpage <= 1) {
					previouspage = 1;
				} else {
					previouspage = currentpage - 1;
				}
				//设置下一页
				if(currentpage >= pagercounts) {
					nextpage = pagercounts;
				} else {
					nextpage = currentpage + 1;
				}

				res.render('manager/manager_productlist', {
					goodslist: results.goodListPager,
					currentpage: currentpage,
					previouspage: previouspage,
					nextpage: nextpage,
					pageSum: pagercounts,
					userInfo: results.userInfo
				});
			});
	});

	//添加产品页面
	app.get('/manager/add_product', function(req, res) {

		var userInfo = req.session.manager;
		//		//验证登录
		//		if(!userInfo) {
		//			res.redirect('/manager/login');
		//		}
		//后台登录地址
		//应用async模块的api:parallel处理并行无关联操作
		async.parallel({ //控制流程1:多级菜单加载
			categorylist: function(done) {
				//处理逻辑
				categoryDAL.getCategoryAll(function(catelist) {

					var mylist = createSelectList(catelist, 0);

					done(null, mylist);
				});

			}
		}, function(err, results) {

			//console.log(results.categorylist);

			res.render('manager/add_product', {
				categorylist: results.categorylist,
				userInfo: userInfo
			});
		});

	});

	//添加产品提交方法
	//重点需要完善和修复
	app.post('/manager/add_goodshandle', function(req, res) {

		var userInfo = req.session.manager;
		//验证登录
		if(!userInfo) {
			res.redirect('/manager/login');
		}

		//设置上传目录
		var uploadPath = './public/upload/goods_img/';
		//设置数据库保存的根目录
		var uploadInsertPath = '/upload/goods_img/';

		//调用自定义模块
		fileuploadHelper.mutlifileUpload(req, uploadPath, uploadInsertPath, function(err, fields, img_first, imglist) {
			//获取表单数据
			var cate_id = fields.category; //产品分类id
			var goods_name = fields.goods_name; //产品名称
			var old_price = fields.old_price; //原价格
			var new_price = fields.new_price; //现价格
			var goods_amount = fields.goods_amount; //库存数量 			
			var imglist = imglist; //上传图片列表字符串
			var good_details = fields.content; //产品详情
			var favourable_price = fields.favourable_price; //优惠价格
			var product_expressfee = fields.goods_expressfee; //快递费用
			//console.log(cate_id + ',' + goods_name + ',' + imglist+','+good_details);
			//实例化对象并赋值
			var pdInfo = new ProductInfo();
			pdInfo.setpdName(goods_name); //产品名称			
			pdInfo.setold_price(old_price); //原价格
			pdInfo.setnew_price(new_price); //现价格	
			pdInfo.setproduct_favourable(favourable_price); //优惠价格
			pdInfo.setproduct_expressfee(product_expressfee); //快递费用
			pdInfo.setproduct_img(img_first); //上传图片第一张图片
			pdInfo.setproduct_imglist(imglist); //上传图片列表字符串
			pdInfo.setproduct_amount(goods_amount); //库存数量
			pdInfo.setproduct_detail(good_details); //产品详情
			pdInfo.setcategory_Id(cate_id); //产品分类id

			console.log('测试:' + pdInfo.getpdName());
			//数据库添加方法
			productDAL.addGoods(pdInfo, function(result) {

				var link_url = "/manager/manager_productlist";
				var message = "";
				if(result) {
					//添加成功
					message = "添加成功";
					res.render('manager/message', {
						link_url: link_url,
						message: message,
						userInfo,
						userInfo
					});
				} else {
					//添加失败
					message = "添加失败";
					res.render('manager/message', {
						link_url: link_url,
						message: message,
						userInfo: userInfo
					});
				}
			});

		});

	});

	//添加分类页面
	app.get('/manager/add_category', function(req, res) {

		var userInfo = req.session.manager;
		//验证登录
		if(!userInfo) {
			res.redirect('/manager/login');
		}

		categoryDAL.getCategoryAll(function(catelist) {

			var mylist = createSelectList1(catelist, 0);

			res.render('manager/add_category', {
				categorylist: mylist,
				userInfo: userInfo
			});

		});

	});

	//提交添加分类方法
	app.post('/manager/add_cate', function(req, res) {
		var up_id = req.body.up_id;
		var cate_name = req.body.catename;

		var userInfo = req.session.manager;
		//验证登录
		if(!userInfo) {
			res.redirect('/manager/login');
		}

		categoryDAL.insertCategoryById(up_id, cate_name, function(count) {

			var link_url = "/manager/add_category";
			var message = "";
			if(count) {
				//添加成功
				message = "添加成功";
				res.render('manager/message', {
					link_url: link_url,
					message: message,
					userInfo: userInfo
				});
			} else {
				//添加失败
				message = "添加失败";
				res.render('manager/message', {
					link_url: link_url,
					message: message,
					userInfo: userInfo
				});
			}
		});

		//		console.log(up_id+','+cate_name);
	});

	//管理分类页面
	app.get('/manager/manager_category', function(req, res) {

		var userInfo = req.session.manager;
		//验证登录
		if(!userInfo) {
			res.redirect('/manager/login');
		}

		categoryDAL.getCategoryAll(function(catelist) {

			var mylist = createSelectList(catelist, 0);

			res.render('manager/manager_category', {
				categorylist: mylist,
				userInfo: userInfo
			});

		});
	});

	//分类管理修改提交方法
	app.post('/manager/modify_category', function(req, res) {

		var userInfo = req.session.manager;
		//验证登录
		if(!userInfo) {
			res.redirect('/manager/login');
		}

		var cate_id = req.body.cateid;
		var cate_name = req.body.catename;
		var up_id = req.body.cate_pid;

		categoryDAL.modifyCategoryById(cate_id, cate_name, up_id, function(count) {

			var link_url = "/manager/manager_category";
			var message = "";
			if(count) {
				//更新成功
				message = "更新成功";
				//res.redirect('/manager/manager_category');
				res.render('manager/message', {
					link_url: link_url,
					message: message,
					userInfo: userInfo
				});
			} else {
				//更新失败
				message = "更新失败";
				//res.redirect('/manager/manager_category');
				res.render('manager/message', {
					link_url: link_url,
					message: message,
					userInfo: userInfo
				});
			}
		});

		//console.log(cate_id + ',' + cate_name);
	});

	//消息页面
	app.get('/manager/message', function(req, res) {

		var userInfo = req.session.manager;
		//验证登录
		if(!userInfo) {
			res.redirect('/manager/login');
		}
		res.render('manager/message');
	});

	//多级分类列表函数
	function createSelectList1(data, rootId) {
		var mylist = []; //存生成的多级分类列表项
		mylist.push({
			category_Id: 0,
			category_name: '根节点'
		});
		createSelectOption(mylist, data, 0);
		return mylist;
	}

	function createSelectList(data, rootId) {
		var mylist = []; //存生成的多级分类列表项
		mylist.push({
			category_Id: 0,
			category_name: '选择分类'
		});
		createSelectOption(mylist, data, 0);
		return mylist;
	}

	//优化方法
	function createSelectOption(select, data, pid) {
		data.map(function(item) {
			if(item.up_id == pid) {
				//添加一级分类列表项
				select.push(item);

				createChildrenNode(select, data, item);
			}
		});
	}

	//创建子节点函数
	function createChildrenNode(select, data, current) {
		data.map(function(item) {
			var blank = "--";
			if(current.category_Id == item.up_id) {
				//item为当前节点的子节点，则计算深度并添加列表项
				intLevel = 0;
				//计算当前节点到根节点的深度，添加深度提示信息
				var intlvl = getLevel(data, 0, current);
				for(a = 0; a < intlvl; a++) {
					blank += "--";
				}

				blank += "|";
				//添加当前节点下列表项
				select.push({
					category_Id: item.category_Id,
					category_name: blank + item.category_name
				});

				createChildrenNode(select, data, item);
			}
		});
	}

	//目录深度方法
	function getLevel(datasources, topId, currentitem) {
		var up_id = currentitem.up_id;
		if(up_id != topId) {
			for(i = 0; i < datasources.length; i++) {
				if(datasources[i].category_Id == up_id) {
					intLevel++;
					getLevel(datasources, topId, datasources[i]);
				}
			}
		}
		return intLevel;
	}

	//修改商品页面
	app.get('/manager/modify_product/:id', function(req, res) {

		var userInfo = req.session.manager;
		//验证登录
		if(!userInfo) {
			res.redirect('/manager/login');
		}

		var product_Id = req.params.id;

		//后台登录地址
		//应用async模块的api:parallel处理并行无关联操作
		async.parallel({ //控制流程1:多级菜单加载
			categorylist: function(done) {
				//处理逻辑
				categoryDAL.getCategoryAll(function(catelist) {
					var mylist = createSelectList(catelist, 0);

					done(null, mylist);
				});
			}, //控制流程2：根据商品ID查询指定商品信息
			productInfo: function(done) {
				productDAL.getGoodsById(product_Id, function(pdInfo) {
					done(null, pdInfo[0]);
				});
			}
		}, function(err, results) {

			res.render('manager/modify_product', {
				categorylist: results.categorylist,
				productInfo: results.productInfo,
				userInfo: userInfo
			});
		});

	});

	//编辑商品处理函数
	app.post('/manager/modify_goodshandle', function(req, res) {
		var userInfo = req.session.manager;
		//验证登录
		if(!userInfo) {
			res.redirect('/manager/login');
		}

		//设置上传目录
		var uploadPath = './public/upload/goods_img/';
		//设置数据库保存的根目录
		var uploadInsertPath = '/upload/goods_img/';

		//调用自定义模块
		fileuploadHelper.mutlifileUpload(req, uploadPath, uploadInsertPath, function(err, fields, img_first, imglist) {
			//获取表单数据
			var product_Id = fields.product_Id //产品ID
			var cate_id = fields.category; //产品分类id
			var goods_name = fields.goods_name; //产品名称
			var old_price = fields.old_price; //原价格
			var new_price = fields.new_price; //现价格
			var goods_amount = fields.goods_amount; //库存数量 			
			var imglist = imglist; //上传图片列表字符串
			var good_details = fields.content; //产品详情
			var favourable_price = fields.favourable_price; //优惠价格
			var product_expressfee = fields.goods_expressfee; //快递费用
			console.log('pd_id' + product_Id);
			//实例化对象并赋值
			var pdInfo = new ProductInfo();
			pdInfo.setpdName(goods_name); //产品名称			
			pdInfo.setold_price(old_price); //原价格
			pdInfo.setnew_price(new_price); //现价格	
			pdInfo.setproduct_favourable(favourable_price); //优惠价格
			pdInfo.setproduct_expressfee(product_expressfee); //快递费用
			pdInfo.setproduct_img(img_first); //上传图片第一张图片
			pdInfo.setproduct_imglist(imglist); //上传图片列表字符串
			pdInfo.setproduct_amount(goods_amount); //库存数量
			pdInfo.setproduct_detail(good_details); //产品详情
			pdInfo.setcategory_Id(cate_id); //产品分类id

			console.log('测试:' + pdInfo.getpdName());
			//数据库添加方法
			productDAL.updateGoodsByID(product_Id, pdInfo, function(result) {

				var link_url = "/manager/manager_productlist";
				var message = "";
				if(result) {
					//添加成功
					message = "更新成功";
					res.render('manager/message', {
						link_url: link_url,
						message: message,
						userInfo,
						userInfo
					});
				} else {
					//添加失败
					message = "更新失败";
					res.render('manager/message', {
						link_url: link_url,
						message: message,
						userInfo: userInfo
					});
				}
			});

		});

	});

	//订单管理页面
	app.get('/manager/manager_orderlist', function(req, res) {
		res.redirect('/manager/manager_orderlist/1');
	});

	//订单管理页面
	app.get('/manager/manager_orderlist/:id', function(req, res) {

		var currentpage = parseInt(req.params.id);

		//验证登录
		if(!req.session.manager) {
			res.redirect('/manager/login');
		}

		//分页方法
		//定义分页参数
		//var currentpage = 1; //当前页
		var pagercounts = 1; //页面总数
		var previouspage = 1; //上一页
		var nextpage = 1;
		//设置分页 每页显示数据条数
		var pageSize = 10;
		//应用async模块的api:parallel处理并行无关联操作
		//async的waterfall模块， 每一步执行时需要由上一步执行的结果当做参数
		async.parallel({
				orderListPager: function(done) {
					//处理逻辑
					orderDAL.getOrderListPager(currentpage, pageSize, function(orderlist) {

						done(null, orderlist);
					});
				},
				orderCounts: function(done) {
					//处理逻辑
					orderDAL.getOrdersCounts(function(count) {
						done(null, count);
					});
				},
				userInfo: function(done) {
					var userInfo = req.session.manager;
					done(null, userInfo);
				}
			},
			function(error, results) {
				//console.log('流程3：');
				//console.log('****'+results.goodsCounts+',');
				//将上面两步数据操作并行处理，然后在这里一起操作，提供运行效率
				var counts = parseInt(results.orderCounts);

				//分页信息处理
				//计算总页数
				if((counts % pageSize) > 0) {
					//有余数，数据不足一整页
					pagercounts = parseInt(counts / pageSize) + 1;
				} else {
					//数据正好填满整页
					pagercounts = parseInt(counts / pageSize);
				}
				//设置上一页
				if(currentpage <= 1) {
					previouspage = 1;
				} else {
					previouspage = currentpage - 1;
				}
				//设置下一页
				if(currentpage >= pagercounts) {
					nextpage = pagercounts;
				} else {
					nextpage = currentpage + 1;
				}

				res.render('manager/manager_orderlist', {
					orderlist: results.orderListPager,
					currentpage: currentpage,
					previouspage: previouspage,
					nextpage: nextpage,
					pageSum: pagercounts,
					userInfo: results.userInfo
				});
			});
	});

	//订单搜索页面
	app.get('/manager/manager_ordersearch', function(req, res) {
		var q = req.query.q;
		var currentpage = parseInt(req.query.p);

		if(q == null || currentpage == null) {

			res.redirect('/manager/manager_ordersearch?q=&js=1');
			return;
		}

		//验证登录
		var managerInfo = req.session.manager;
		if(!managerInfo) {
			res.redirect('/manager/login');
			return;
		}

		//分页方法
		//定义分页参数
		//var currentpage = 1; //当前页
		var pagercounts = 1; //页面总数
		var previouspage = 1; //上一页
		var nextpage = 1;
		//设置分页 每页显示数据条数
		var pageSize = 20;

		//如果搜索结果为空则返回空数据集
		if(q == null || q == '') {
			currentpage = 1;
			q = '';
			res.render('manager/manager_ordersearch', {
				orderlist: '',
				currentpage: currentpage,
				previouspage: previouspage,
				nextpage: nextpage,
				pageSum: pagercounts,
				q: q,
				userInfo: managerInfo
			});
		} else {
			//应用async模块的api:parallel处理并行无关联操作
			async.parallel({ //控制流程1:搜索
				orderlistPager: function(done) {

					orderDAL.getSearchOrderListPager(q, currentpage, pageSize, function(orderlist) {
						done(null, orderlist);
					});
				}, //控制流程2:用户信息
				userInfo: function(done) {
					var managerInfo = req.session.manager;
					done(null, managerInfo);
				}, //控制流程3 当前分类商品总数
				orderCounts: function(done) {
					orderDAL.getSearchOrdersCounts(q, function(count) {
						done(null, count);
					});
				}
			}, function(err, results) {

				//将上面几个步骤数据操作并行处理，然后在这里一起操作，提供运行效率
				var counts = parseInt(results.orderCounts);

				//分页信息处理
				//计算总页数
				if((counts % pageSize) > 0) {
					//有余数，数据不足一整页
					pagercounts = parseInt(counts / pageSize) + 1;
				} else {
					//数据正好填满整页
					pagercounts = parseInt(counts / pageSize);
				}
				//设置上一页
				if(currentpage <= 1) {
					previouspage = 1;
				} else {
					previouspage = currentpage - 1;
				}
				//设置下一页
				if(currentpage >= pagercounts) {
					nextpage = pagercounts;
				} else {
					nextpage = currentpage + 1;
				}

				res.render('manager/manager_ordersearch', {
					orderlist: results.orderlistPager,
					currentpage: currentpage,
					previouspage: previouspage,
					nextpage: nextpage,
					pageSum: pagercounts,
					q: q,
					userInfo: results.userInfo
				});
			});
		}

	});

	app.get('/manager/manager_userlist', function(req, res) {
		res.redirect('/manager/manager_userlist/1');
	});

	//用户管理列表
	app.get('/manager/manager_userlist/:id', function(req, res) {
		var currentpage = parseInt(req.params.id);

		//验证登录
		if(!req.session.manager) {
			res.redirect('/manager/login');
		}

		//分页方法
		//定义分页参数
		//var currentpage = 1; //当前页
		var pagercounts = 1; //页面总数
		var previouspage = 1; //上一页
		var nextpage = 1;
		//设置分页 每页显示数据条数
		var pageSize = 10;
		//应用async模块的api:parallel处理并行无关联操作
		//async的waterfall模块， 每一步执行时需要由上一步执行的结果当做参数
		async.parallel({
				userListPager: function(done) {
					//处理逻辑
					customerDAL.getCustomerListPager(currentpage, pageSize, function(goodslist) {

						done(null, goodslist);
					});
				},
				userCounts: function(done) {
					//处理逻辑
					customerDAL.getCustomerCounts(function(count) {
						done(null, count);
					});
				},
				userInfo: function(done) {
					var userInfo = req.session.manager;
					done(null, userInfo);
				}
			},
			function(error, results) {
				//console.log('流程3：');
				//console.log('****'+results.goodsCounts+',');
				//将上面两步数据操作并行处理，然后在这里一起操作，提供运行效率
				var counts = parseInt(results.userCounts);

				//分页信息处理
				//计算总页数
				if((counts % pageSize) > 0) {
					//有余数，数据不足一整页
					pagercounts = parseInt(counts / pageSize) + 1;
				} else {
					//数据正好填满整页
					pagercounts = parseInt(counts / pageSize);
				}
				//设置上一页
				if(currentpage <= 1) {
					previouspage = 1;
				} else {
					previouspage = currentpage - 1;
				}
				//设置下一页
				if(currentpage >= pagercounts) {
					nextpage = pagercounts;
				} else {
					nextpage = currentpage + 1;
				}

				res.render('manager/manager_userlist', {
					userlist: results.userListPager,
					currentpage: currentpage,
					previouspage: previouspage,
					nextpage: nextpage,
					pageSum: pagercounts,
					userInfo: results.userInfo
				});
			});
	});

	//用户查询页面
	app.get('/manager/manager_usersearch', function(req, res) {
		var q = req.query.q;
		var currentpage = parseInt(req.query.p);

		if(q == null || currentpage == null) {

			res.redirect('/manager/manager_usersearch?q=&js=1');
		}

		//验证登录
		var managerInfo = req.session.manager;
		if(!managerInfo) {
			res.redirect('/manager/login');
		}

		//分页方法
		//定义分页参数
		//var currentpage = 1; //当前页
		var pagercounts = 1; //页面总数
		var previouspage = 1; //上一页
		var nextpage = 1;
		//设置分页 每页显示数据条数
		var pageSize = 20;

		//如果搜索结果为空则返回空数据集
		if(q == null || q == '') {
			currentpage = 1;
			q = '';
			res.render('manager/manager_usersearch', {
				userlist: '',
				currentpage: currentpage,
				previouspage: previouspage,
				nextpage: nextpage,
				pageSum: pagercounts,
				q: q,
				userInfo: managerInfo
			});
		} else {
			//应用async模块的api:parallel处理并行无关联操作
			async.parallel({ //控制流程1:搜索
				userlistPager: function(done) {

					customerDAL.getSearchCustomerListPager(q, currentpage, pageSize, function(orderlist) {
						done(null, orderlist);
					});
				}, //控制流程2:用户信息
				userInfo: function(done) {
					var managerInfo = req.session.manager;
					done(null, managerInfo);
				}, //控制流程3 当前分类商品总数
				userCounts: function(done) {
					customerDAL.getSearchCustomerCounts(q, function(count) {
						done(null, count);
					});
				}
			}, function(err, results) {

				//将上面几个步骤数据操作并行处理，然后在这里一起操作，提供运行效率
				var counts = parseInt(results.userCounts);

				//分页信息处理
				//计算总页数
				if((counts % pageSize) > 0) {
					//有余数，数据不足一整页
					pagercounts = parseInt(counts / pageSize) + 1;
				} else {
					//数据正好填满整页
					pagercounts = parseInt(counts / pageSize);
				}
				//设置上一页
				if(currentpage <= 1) {
					previouspage = 1;
				} else {
					previouspage = currentpage - 1;
				}
				//设置下一页
				if(currentpage >= pagercounts) {
					nextpage = pagercounts;
				} else {
					nextpage = currentpage + 1;
				}

				res.render('manager/manager_usersearch', {
					userlist: results.userlistPager,
					currentpage: currentpage,
					previouspage: previouspage,
					nextpage: nextpage,
					pageSum: pagercounts,
					q: q,
					userInfo: results.userInfo
				});
			});
		}
	});

	//管理员密码修改页面
	app.get('/manager/manager_modifypass', function(req, res) {
		//验证登录
		if(!req.session.manager) {
			res.redirect('/manager/login');
		}

		//应用async模块的api:parallel处理并行无关联操作
		async.parallel({ //控制流程1:管理员用户信息			
			userInfo: function(done) {
				var userInfo = req.session.manager;
				done(null, userInfo);
			}
		}, function(err, results) {
			//后台登录地址
			res.render('manager/manager_modifypass', {
				userInfo: results.userInfo
			});

		});
	});

	//管理员密码修改处理方法
	app.post('/manager/manager_modifypasshandle', function(req, res) {

		var userInfo = req.session.manager;
		var oldpass = req.body.oldpass;
		var newpass = req.body.newpass;
		//验证登录
		if(!userInfo) {
			res.status(200).send({
				url: '/manager/login',
				message: '未登录'
			});
		} else {
			managerDAL.modifyManagerPass(userInfo.username, oldpass, newpass, function(message, results) {
				var link_url = "/manager/";
				res.status(200).send({
					url: link_url,
					message: message
				});
			});
		}
	});
}

module.exports = managerController;